package com.example.demo.service.serviceImpl;

import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.FavoriteAuthor;
import com.example.demo.entity.requestJson.AFavoriteAuthorReq;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.requestJson.IsFavoriteReq;
import com.example.demo.entity.responseJson.AFollowingRes;
import com.example.demo.entity.responseJson.ListResult;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.FavoriteAuthorMapper;
import com.example.demo.service.AFavoriteAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/3/6 15:26
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class AFavoriteAuthorServiceImpl implements AFavoriteAuthorService {
    @Autowired
    private FavoriteAuthorMapper favoriteAuthorMapper;
    @Autowired
    private AuthorMapper authorMapper;

    /**
     * 在作者主页关注作者
     */

    @Override
    public BackResponseEntity<Object> favoriteAuthor(AFavoriteAuthorReq aFavoriteAuthorReq){
        String userId = null;
        String authorId = null;

        if(!ObjectUtils.isEmpty(aFavoriteAuthorReq.getAuthorId())){
            authorId = aFavoriteAuthorReq.getAuthorId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(aFavoriteAuthorReq.getUserId())){
            userId = aFavoriteAuthorReq.getUserId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(authorId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        if(authorId.equals(userId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1016, "请不要关注自己");
        }

        FavoriteAuthor favoriteAuthor = new FavoriteAuthor();
        favoriteAuthor.setAuthorId(authorId);
        favoriteAuthor.setUserId(userId);
        favoriteAuthor.setUserRole(0);
        favoriteAuthor.setFavoriteTime(LocalDateTime.now());

        int i = favoriteAuthorMapper.insert(favoriteAuthor);

        if(i > 0){

            Author author = new Author();
            int count = favoriteAuthorMapper.queryAttentioncount(userId) +1;
            author.setAuthorId(userId);
            author.setAttentionCount(count);

            int m = authorMapper.updateAuthor(author);

            Author author1 = new Author();
            int readerCount = favoriteAuthorMapper.queryReadercount(authorId) +1;
            author1.setAuthorId(authorId);
            author1.setReaderCount(readerCount);

            int n = authorMapper.updateAuthor(author1);

            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "关注成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1013, "关注失败");
        }
    }

    /**
     * 是否关注该作者
     * */
    @Override
    public BackResponseEntity<Object> isfavoriteAuthor(IsFavoriteReq isFavoriteReq){

        if(ObjectUtils.isEmpty(isFavoriteReq.getUserId()) || ObjectUtils.isEmpty(isFavoriteReq.getAuthorId())){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }
        if(favoriteAuthorMapper.isFavoriteAuthor(isFavoriteReq) == 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_1015, "未关注该作者");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1014, "已关注该作者");
        }
    }

    /**
    * 查看我的关注列表
    * */
    @Override
    public BackResponseEntity<ListResult<List<AFollowingRes>>> queryAFollowing(DeleteReq deleteReq){
        List<AFollowingRes> list  = favoriteAuthorMapper.queryAFollowing(deleteReq.getId());


        int count = favoriteAuthorMapper.queryAttentioncount(deleteReq.getId());
        ListResult<List<AFollowingRes>> listResult = new ListResult((int)count, list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");
    }

    /**
    * 取消关注
    * 1.进行取消操作
    * 2.相应的关注数和读者数减少
    * 3.删除关注表中的记录
    * */
    @Override
    public BackResponseEntity<Object> unSubscribe(IsFavoriteReq isFavoriteReq){
        String userId = null;
        String authorId = null;

        if(!ObjectUtils.isEmpty(isFavoriteReq.getAuthorId())){
            authorId = isFavoriteReq.getAuthorId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(isFavoriteReq.getUserId())){
            userId = isFavoriteReq.getUserId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(authorId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }
        FavoriteAuthor favoriteAuthor = new FavoriteAuthor();
        favoriteAuthor.setAuthorId(authorId);
        favoriteAuthor.setUserId(userId);
        int i = favoriteAuthorMapper.deleteFavoriteAuthorByEntity(favoriteAuthor);

        if( i > 0 ){
            Author author = new Author();
            int count = favoriteAuthorMapper.queryAttentioncount(userId) -1;
            author.setAuthorId(userId);
            author.setAttentionCount(count);

            int m = authorMapper.updateAuthor(author);

            Author author1 = new Author();
            int readerCount = favoriteAuthorMapper.queryReadercount(authorId) -1;
            author1.setAuthorId(authorId);
            author1.setReaderCount(readerCount);

            int n = authorMapper.updateAuthor(author1);

            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "取消关注成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1017, "取消关注失败");
        }

    }

    /**
     * 查看我的读者列表
     * 1.读者是作者
     * 2.读者是普通用户
     * */
    @Override
    public BackResponseEntity<ListResult<List<AFollowingRes>>> queryReader(DeleteReq deleteReq){
        List<AFollowingRes> list  = favoriteAuthorMapper.queryReaderAuthor(deleteReq.getId());
        List<AFollowingRes> list1 = favoriteAuthorMapper.queryReaderUser(deleteReq.getId());


        List<AFollowingRes> listres = new ArrayList<>();
        listres.addAll(list1);
        listres.addAll(list);

        int count = favoriteAuthorMapper.queryReadercount(deleteReq.getId());
        ListResult<List<AFollowingRes>> listResult = new ListResult((int)count, listres);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");
    }

}
